package com.example.qixinshangwutong.view.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.qixinshangwutong.BaseApplication;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.model.home.HomeBrandRequest;
import com.example.qixinshangwutong.model.home.HomeBrandValues;
import com.example.qixinshangwutong.model.home.HomeNewsAdapter;
import com.example.qixinshangwutong.model.home.HomeNewsModel;
import com.example.qixinshangwutong.model.home.HomeNewsRequest;
import com.example.qixinshangwutong.model.home.HomeNewsValues;
import com.example.qixinshangwutong.model.home.HotWorldRequest;
import com.example.qixinshangwutong.model.home.HotWorldValue;
import com.example.qixinshangwutong.model.home.RecycleAdapter;
import com.example.qixinshangwutong.model.home.RecyclePictureAdapter;
import com.example.qixinshangwutong.model.home.RecyclePictureModel;
import com.example.qixinshangwutong.model.home.RecycleTextModel;
import com.example.qixinshangwutong.view.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeFragment extends Fragment {
    private TextView search;
    private RecyclerView brandName;
    private ImageView searchBrand;
    private ImageView searchType;
    private ImageView searchCompany;
    private ImageView searchApp;
    private TextView moreBrand;
    private RecyclerView brandImage;
    private TextView moreNews;
    private RecyclerView news;
    private Intent intent;
    private TextView changeBrand;
    private onMoreClickListener listener;

    public void setListener(onMoreClickListener listener) {
        this.listener = listener;
    }

    public onMoreClickListener getListener() {
        return listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root=inflater.inflate(R.layout.fragment_home, container, false);
        initView(root);
        clickSearch();
        changeBrandName();
        clickBrandName();
        clickType();
        clickCompany();
        clickMore();
        recycleHotWorld();
        recycleBrand();
        recycleNews();

        return root;
    }
    private void initView(View root){
        search=root.findViewById(R.id.text_select);
        brandName=root.findViewById(R.id.recycle_brand_name);
        changeBrand=root.findViewById(R.id.change_text);
        searchBrand=root.findViewById(R.id.select_pinpai);
        searchType=root.findViewById(R.id.select_hangye);
        searchCompany=root.findViewById(R.id.select_qiye);
        searchApp=root.findViewById(R.id.select_app);
        moreBrand=root.findViewById(R.id.more_pinpai);
        brandImage=root.findViewById(R.id.recycle_image);
        moreNews=root.findViewById(R.id.more_news);
        news=root.findViewById(R.id.recycle_news);
        intent=new Intent(requireActivity(), SearchActivity.class);
    }
    private void changeBrandName(){
        changeBrand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recycleHotWorld();
            }
        });
    }
//点击搜索后跳转
    private void clickSearch(){
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });

    }
    //点击搜品牌后跳转
    private void clickBrandName(){
        searchBrand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }
    //点击搜行业后跳转
    private void clickType(){
        searchType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }
    //点击搜企业后跳转
    private void clickCompany(){
        searchCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }

    //热词
    private void recycleHotWorld(){
        LinearLayoutManager llm=new LinearLayoutManager(requireActivity());
        llm.setOrientation(RecyclerView.HORIZONTAL);
        brandName.setLayoutManager(llm);
        List<RecycleTextModel> list=new ArrayList<>();
        RecycleAdapter adapter=new RecycleAdapter(requireActivity());
        adapter.setData(list);
        brandName.setAdapter(adapter);
        Call<HotWorldRequest<HotWorldValue>> call= BaseApplication.api.getHotWorldData("android");
        call.enqueue(new Callback<HotWorldRequest<HotWorldValue>>() {
            @Override
            public void onResponse(Call<HotWorldRequest<HotWorldValue>> call, Response<HotWorldRequest<HotWorldValue>> response) {
                for (int i = 0; i <response.body().getValues().getData().size() ; i++) {
                    RecycleTextModel model=new RecycleTextModel();
                    model.setId(response.body().getValues().getData().get(i).getMapping_id());
                    model.setName(response.body().getValues().getData().get(i).getTitle());
                    model.setUrl(response.body().getValues().getData().get(i).getUrl_wap());
                    list.add(model);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<HotWorldRequest<HotWorldValue>> call, Throwable t) {

            }
        });
    }
    private void recycleBrand(){
        LinearLayoutManager llm=new LinearLayoutManager(requireActivity());
        llm.setOrientation(RecyclerView.HORIZONTAL);
        brandImage.setLayoutManager(llm);
        List<RecyclePictureModel> list=new ArrayList<>();
        RecyclePictureAdapter adapter=new RecyclePictureAdapter(requireActivity());
        adapter.setData(list);
        brandImage.setAdapter(adapter);
        Call<HomeBrandRequest<HomeBrandValues>> call=BaseApplication.api.getHomeBrand();
        call.enqueue(new Callback<HomeBrandRequest<HomeBrandValues>>() {
            @Override
            public void onResponse(Call<HomeBrandRequest<HomeBrandValues>> call, Response<HomeBrandRequest<HomeBrandValues>> response) {
                for (int i = 0; i <response.body().getValues().getTui_brand().size() ; i++) {
                    RecyclePictureModel model=new RecyclePictureModel();
                    model.setId(response.body().getValues().getTui_brand().get(i).getMapping_id());
                    model.setPname(response.body().getValues().getTui_brand().get(i).getCompany_name());
                    model.setPurl(response.body().getValues().getTui_brand().get(i).getUrl());
                    list.add(model);

                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<HomeBrandRequest<HomeBrandValues>> call, Throwable t) {

            }
        });

    }
    private void recycleNews(){
        LinearLayoutManager llm=new LinearLayoutManager(requireActivity());
        llm.setOrientation(RecyclerView.VERTICAL);
        news.setLayoutManager(llm);
        List<HomeNewsModel> list=new ArrayList<>();
        HomeNewsAdapter adapter=new HomeNewsAdapter(requireActivity());
        adapter.setNewsData(list);
        news.setAdapter(adapter);
        Call<HomeNewsRequest<HomeNewsValues>> call=BaseApplication.api.getHomeNews();
        call.enqueue(new Callback<HomeNewsRequest<HomeNewsValues>>() {
            @Override
            public void onResponse(Call<HomeNewsRequest<HomeNewsValues>> call, Response<HomeNewsRequest<HomeNewsValues>> response) {
                for (int i = 0; i <response.body().getValues().getTui_baodao_news().size() ; i++) {
                    HomeNewsModel model=new HomeNewsModel();
                    model.setId(response.body().getValues().getTui_baodao_news().get(i).getId());
                    model.setUrl(response.body().getValues().getTui_baodao_news().get(i).getUrl());
                    model.setTitle(response.body().getValues().getTui_baodao_news().get(i).getTitle());
                    model.setTime(response.body().getValues().getTui_baodao_news().get(i).getAdd_time());
                    list.add(model);

                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<HomeNewsRequest<HomeNewsValues>> call, Throwable t) {

            }
        });

    }
    private void clickMore(){
        moreBrand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    listener.onItemClick();

            }
        });
        moreNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    listener.onNewsClick();

            }
        });
    }
    public interface onMoreClickListener {
        void onItemClick();
        void onNewsClick();
    }
}